package com.wq.base.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.base.entity.Label;
import com.wq.base.mapper.LabelMapper;
import com.wq.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class LabelService {
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 根据id查询Label
     *
     * @param id
     * @return
     */
    public Label queryById(String id) {
        return labelMapper.selectById(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<Label> findAll() {
        return labelMapper.selectList(null);
    }

    /**
     * 新增
     *
     * @param label
     */
    public void save(Label label) {
        label.setId(idWorker.nextId() + "");
        labelMapper.insert(label);
    }

    /**
     * 更新
     *
     * @param label
     */
    public void update(Label label) {
        labelMapper.updateById(label);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(String id) {
        labelMapper.deleteById(id);
    }

    /**
     * 带条件分页查询
     *
     * @param map
     * @param page
     * @param size
     * @return
     */
    public Page search(Map map, int page, int size) {
//name：zs；age：18
        QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
        Set<String> set = map.keySet();
        for (String s : set) {
            queryWrapper.eq(null != map.get(s), s, map.get(s));
        }
        Page<Label> labelPage = new Page<>(page, size);
        Page<Label> page1 = labelMapper.selectPage(labelPage, queryWrapper);
        return page1;
    }
}